package com.hjx.aes;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AesTestApp {

	private JFrame frmAes;
	private JTextField txt_password;
	private JTextArea txt_content;
	private JTextArea txt_aes;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					AesTestApp window = new AesTestApp();
					window.frmAes.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public AesTestApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmAes = new JFrame();
		frmAes.setTitle("AES加密解密");
		frmAes.setBounds(100, 100, 872, 450);
		frmAes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frmAes.getContentPane().add(panel, BorderLayout.NORTH);
		
		JLabel label = new JLabel("密码");
		panel.add(label);
		
		txt_password = new JTextField();
		panel.add(txt_password);
		txt_password.setColumns(80);
		
		JButton button = new JButton("加密");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String password = txt_password.getText();
				String content = txt_content.getText();
				if(password==null||content==null){
					return;
				}
				byte[] encrypt = AES256.Aes256Encode(content, AES256.parseHexStr2Byte(password));
				String byte2HexStr = ParseSystemUtil.parseByte2HexStr(encrypt);
				txt_aes.setText(byte2HexStr);
			}
		});
		panel.add(button);
		
		JButton button_1 = new JButton("解密");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String password = txt_password.getText();
				String content = txt_content.getText();
				if(password==null||content==null){
					return;
				}
				String s = AES256.Aes256Decode(ParseSystemUtil.parseHexStr2Byte(content), AES256.parseHexStr2Byte(password));
				txt_aes.setText(s);
			}
		});
		panel.add(button_1);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.5);
		frmAes.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		splitPane.setLeftComponent(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		txt_content = new JTextArea();
		txt_content.setToolTipText("HEX字符串");
		txt_content.setLineWrap(true);
		JScrollPane jScrollPane = new JScrollPane(txt_content);
		panel_1.add(jScrollPane, BorderLayout.CENTER);
		
		JPanel panel_2 = new JPanel();
		splitPane.setRightComponent(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		txt_aes = new JTextArea();
		txt_aes.setToolTipText("HEX字符串");
		txt_aes.setLineWrap(true);
		JScrollPane jScrollPane2 = new JScrollPane(txt_aes);
		panel_2.add(jScrollPane2, BorderLayout.CENTER);
	}

}
